package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.VenueReportDrawingHistoryDTO;
import com.eastfair.home.dto.VenueReportDrawingHistoryPageQuery;
import com.eastfair.home.dto.VenueReportDrawingHistorySaveDTO;
import com.eastfair.home.dto.VenueReportDrawingHistoryUpdateDTO;
import com.eastfair.home.entity.VenueReportDrawingHistory;
import com.eastfair.home.service.VenueReportDrawingHistoryService;
import com.eastfair.home.vo.VenueReportDrawingHistoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 一键报馆图纸提交记录
 * </p>
 *
 * @author dq
 * @date 2022-10-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/venueReportDrawingHistory")
@Api(value = "VenueReportDrawingHistory", tags = "一键报馆图纸提交记录")
@PreAuth(replace = "home:venueReportDrawingHistory:")
public class VenueReportDrawingHistoryController extends SuperController<VenueReportDrawingHistoryService, Long, VenueReportDrawingHistory, VenueReportDrawingHistoryPageQuery, VenueReportDrawingHistorySaveDTO, VenueReportDrawingHistoryUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private VenueReportDrawingHistoryService venueReportDrawingHistoryService;

    @Override
    public void handlerResult(IPage<VenueReportDrawingHistory> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<VenueReportDrawingHistory> venueReportDrawingHistoryList = list
                .stream()
                .map((map) -> VenueReportDrawingHistory.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(venueReportDrawingHistoryList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<VenueReportDrawingHistoryPageQuery> params) {
        VenueReportDrawingHistoryPageQuery pageQuery = params.getModel();
    }

    /**
     * 分页查询图纸提交历史VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询图纸提交历史VO")
    @PostMapping("/query/history")
    public R<Page<VenueReportDrawingHistoryVO>> queryHistory(@RequestBody PageParams<VenueReportDrawingHistoryDTO> pageParams) {
        log.info("queryHistory - 分页查询图纸提交历史VO, pageParams={}", pageParams);
        Page<VenueReportDrawingHistoryVO> page = venueReportDrawingHistoryService.listHistoryVO(pageParams);
        return R.successDef(page);
    }

    /**
     * 批量更新提交历史的审核状态.
     *
     * @param list the list
     * @return the r
     */
    @ApiOperation("批量更新提交历史的审核状态")
    @PostMapping("/batch/update/auditStatus")
    public R<Boolean> updateBatchAuditStatus(@RequestBody List<VenueReportDrawingHistoryDTO> list) {
        log.info("updateBatchAuditStatus - 批量更新提交历史的审核状态, list={}", list);
        try {
            return R.successDef(venueReportDrawingHistoryService.updateBatchAuditStatus(list));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
